package com.foreknow.hospital.dao.impl;

import com.foreknow.hospital.dao.SetmealDao;
import com.foreknow.hospital.po.CheckItem;
import com.foreknow.hospital.po.Setmeal;
import com.foreknow.hospital.po.SetmealDetailed;
import com.foreknow.hospital.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class SetmealDaoImplAAA implements SetmealDao {
    private Connection con;
    private PreparedStatement pre;
    private ResultSet rs;
    @Override
    public List<Setmeal> getSetmealByType(int type) throws SQLException {
       List<Setmeal> list = new ArrayList<Setmeal>();
        String sql = "select * from setmeal where type=?";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
            pre.setInt(1, type);
            //3.执行查询操作
            rs = pre.executeQuery();
            while (rs.next()) {
                Setmeal setmeal = new Setmeal();
              setmeal.setSmId(rs.getInt("smId"));
              setmeal.setName(rs.getString("name"));
                setmeal.setType(rs.getInt("type"));
               setmeal.setPrice(rs.getInt("price"));
                list.add(setmeal);
        //        CheckItem checkItem = new CheckItem();
           //    checkItem.


            }
        } finally {
            DBUtil.close(pre);
        }
        return list;
    }

    @Override
    public Setmeal getSetmealById(Setmeal setmeal) throws SQLException {
        int smId = setmeal.getSmId();
        Setmeal result = null;
        List<SetmealDetailed> sdList = new ArrayList<>();
        String sql = "SELECT s.smId,s.name, d.sdId,c.ciId,c.ciName,c.ciContent,c.meaning\n" +
                "from setmeal s ,setmealdetailed d,checkitem  c\n" +
                "WHERE s.smId=d.smId and c.ciId=d.ciId  and s.smId=?";

        try {
            con = DBUtil.getConnection();
            pre = con.prepareStatement(sql);
            pre.setInt(1, smId);
            rs = pre.executeQuery();

            while (rs.next()) {
                // 初始化套餐基础信息
                if (result == null) {
                    result = new Setmeal();
                    result.setSmId(rs.getInt("smId"));
                    result.setName(rs.getString("name"));
                    //result.setType(rs.getInt("type"));
                    //result.setPrice(rs.getInt("price"));
                    result.setSdList(sdList);
                }

                // 封装明细项（SetmealDetailed）
                Integer sdId = rs.getInt("sdId");
                if (sdId != null) {
                    SetmealDetailed sd = new SetmealDetailed();
                    sd.setSdId(sdId);
                    sd.setSmId(rs.getInt("smId"));
                    sd.setCiId(rs.getInt("ciId"));

                    // 封装检查项（CheckItem）
                    CheckItem ci = new CheckItem();
                    ci.setCiId(rs.getInt("ciId"));
                    ci.setCiName(rs.getString("ciName"));
                    ci.setCiContent(rs.getString("ciContent"));
                    ci.setMeaning(rs.getString("meaning"));
                    sd.setCheckItem(ci);
                    result.getSdList().add(sd);
                }
            }
        } finally {
            DBUtil.close(pre);
        }

        return result;
    }
}



